#!/usr/bin/env bash

###########################################################################################################
# v1.0 google api
# /home/rb/bin/translate ru-en
##LANG="$1" #ru-en,en-ru
##notify-send "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xsel -o | sed "s/[\"'<>]//g")&sl=auto&tl=${TL}" | sed 's/\[\[\[\"//' | cut -d \" -f 1)" -i accessories-dictionary -u normal -t 7000

###########################################################################################################
# v2.0 yandex api
# /home/rb/bin/translate ru-en
##LANG="$1" #ru-en,en-ru
##notify-send "${LANG} ($(xsel -o)) :" "$(curl -s -XPOST "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150507T105006Z.339c24be2dd37b61.1c01c078329dfbbe764053747a214f7792e51754&lang=${LANG}" -d "text=$(xsel -o)" | jq -r '.["text"][0]')" -i accessories-dictionary -u normal -t 7000

# v.2.1 yandex api
# /home/rb/bin/translate ru-en
##LANG="$1" #ru-en,en-ru
##notify-send "${LANG} ($(xsel -o)) :" "$(echo -e "$(curl -s -XPOST "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150507T105006Z.339c24be2dd37b61.1c01c078329dfbbe764053747a214f7792e51754&lang=${LANG}" -d "text=$(xsel -o)" | jq -r '.["text"][0]')")" -i accessories-dictionary -u normal -t 7000


###########################################################################################################
# v2.2 yandex api
# /home/rb/bin/translate ru-en
# wrap
##LANG="$1" #ru-en,en-ru
##TEXT="$(xsel -o)"
##TEXT_LEN=${#TEXT}
##WRAP=200
##if [ ${TEXT_LEN} -gt ${WRAP} ] ; then
##	LINE_LEN=${WRAP}
##else
##	LINE_LEN=${TEXT_LEN}
##fi
##
##notify-send "$(echo -e "${LANG^^}\n($(echo "${TEXT}"|fold -sw ${WRAP}))\n$(eval "printf '%0.1s' "_"{1..${LINE_LEN}}")\n")" "$(echo -e "$(curl -s -XPOST "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150507T105006Z.339c24be2dd37b61.1c01c078329dfbbe764053747a214f7792e51754&lang=${LANG}" -d "text=${TEXT}" | jq -r '.["text"][0]' | fold -sw ${WRAP})")" -i accessories-dictionary -u normal -t 7000

###########################################################################################################
# v2.3 ya api
# /home/rb/bin/translate ru-en
# xclip -i -selection clipboard
##LANG="$1" #ru-en,en-ru
##TEXT="$(xsel -o)"
##TEXT_LEN=${#TEXT}
##WRAP=200
##if [ ${TEXT_LEN} -gt ${WRAP} ] ; then
##        LINE_LEN=${WRAP}
##else
##        LINE_LEN=${TEXT_LEN}
##fi
##
##TITLE="$(echo -e "${LANG^^}\n($(echo "${TEXT}"|fold -sw ${WRAP}))\n$(eval "printf '%0.1s' "_"{1..${LINE_LEN}}")\n")"
##BODY="$(echo -e "$(curl -s -XPOST "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150507T105006Z.339c24be2dd37b61.1c01c078329dfbbe764053747a214f7792e51754&lang=${LANG}" -d "text=${TEXT}" | jq -r '.["text"][0]' | fold -sw ${WRAP})")"
##
##echo -n "${BODY}" | xclip -i -selection clipboard
##
##notify-send "${TITLE}" "${BODY}" -i accessories-dictionary -u normal -t 30000

###########################################################################################################
# v2.4 ya api
# /home/rb/bin/translate ru-en
# wiki link added
##TEXT="$(xsel -o)"
##TEXT_LEN=${#TEXT}
##WRAP=200
##if [ ${TEXT_LEN} -gt ${WRAP} ] ; then
##        LINE_LEN=${WRAP}
##else
##        LINE_LEN=${TEXT_LEN}
##fi
##
##TITLE="$(echo -e "${LANG^^}\n($(echo "${TEXT}"|fold -sw ${WRAP}))\n$(eval "printf '%0.1s' "_"{1..${LINE_LEN}}")\n")"
##BODY="$(echo -e "$(curl -s -XPOST "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150507T105006Z.339c24be2dd37b61.1c01c078329dfbbe764053747a214f7792e51754&lang=${LANG}" -d "text=${TEXT}" | jq -r '.["text"][0]' | fold -sw ${WRAP})")"
##
##echo -n "${BODY}" | xclip -i -selection clipboard
##
##notify-send "${TITLE}" "$(echo -e "${BODY}\n\nhttps://ru.wikipedia.org/wiki/${BODY}")" -i accessories-dictionary -u normal -t 30000

###########################################################################################################
# v2.5.0 crow https://github.com/crow-translate/crow-translate/releases
# http://compizomania.blogspot.com/2019/05/crow-translate-ubuntu.html
# xclip -i -selection clipboard
# /home/rb/bin/translate ru en
SLANG="${1}"
TLANG="${2}"

TEXT="$(xsel -o)"
TEXT_LEN=${#TEXT}
WRAP=100
if [ ${TEXT_LEN} -gt ${WRAP} ] ; then
        LINE_LEN=${WRAP}
else
        LINE_LEN=${TEXT_LEN}
fi

#2022-07-23# TITLE="$(echo -e "[ ${SLANG^^} -> ${TLANG^^} ]\n\n($(echo "${TEXT}" | fold -sw ${WRAP}))\n$(eval "printf '%0.1s' "_"{1..${LINE_LEN}}")\n")"
TITLE="$(echo -e "[ ${SLANG^^} -> ${TLANG^^} ]\n\n($(echo "${TEXT}" | fold -sw ${WRAP}))\n$(eval "printf '_%.0s' {1..${LINE_LEN}}")\n")"
BODY="$(echo -e "$(crow -s "${SLANG}" -t "${TLANG}" -b "${TEXT}" | fold -sw ${WRAP})")"

echo -n "${BODY}" | xclip -i -selection clipboard

#notify-send "${TITLE}" "${BODY}" -i accessories-dictionary -u normal -t 30000
notify-send -i accessories-dictionary -u normal -t 30000 -- "${TITLE}" "${BODY}"